﻿CREATE FUNCTION [dbo].[RandString]
(
	@Length int
)
RETURNS nvarchar(max)
AS
BEGIN
	DECLARE @result nvarchar(max) = ''

	DECLARE @ValidCharacters varchar(255) = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
	DECLARE @ValidCharactersLength int
	SET @ValidCharactersLength = len(@ValidCharacters)
	DECLARE @RandomNum tinyint

	DECLARE @counter int = 0
	WHILE @counter < @Length
	BEGIN
		SET @RandomNum = Convert(tinyint, ((@ValidCharactersLength - 1) * dbo.ReturnRand () + 1))

		SET @result = @result + SUBSTRING(@ValidCharacters, @RandomNum, 1)

		SET @counter = @counter + 1
	END

	RETURN @result
END